﻿
namespace Microsoft.Dynamics.AX.Framework.TeamFoundationServerFacade
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using Microsoft.TeamFoundation.VersionControl.Client;

    /// <summary>
    /// Proxy class for WorkItemCheckInInfo
    /// </summary>
    [Serializable]
    public class WorkItemCheckInInfoProxy
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="workItem">A proxy object for TFS Work Item</param>
        /// <param name="action">Check-in action</param>
        public WorkItemCheckInInfoProxy(WorkItemProxy workItem, TFSWorkItemCheckinAction action)
        {
            this.WorkItem = workItem;
            this.Action = action;
        }

        /// <summary>
        /// Related TFS Work Item
        /// </summary>
        public WorkItemProxy WorkItem
        {
            get;
            private set;
        }

        /// <summary>
        /// Related Work Item Checkin Action
        /// </summary>
        public TFSWorkItemCheckinAction Action
        {
            get;
            set;
        }

        /// <summary>
        /// Gets WorkItemCheckinAction from mapped value of Action
        /// </summary>
        /// <returns></returns>
        internal WorkItemCheckinAction GetWorkItemCheckinAction()
        {
            switch (this.Action)
            {
                case TFSWorkItemCheckinAction.Associate:
                    return WorkItemCheckinAction.Associate;
                case TFSWorkItemCheckinAction.None:
                    return WorkItemCheckinAction.None;
                case TFSWorkItemCheckinAction.Resolve:
                    return WorkItemCheckinAction.Resolve;
                default:
                    throw new System.InvalidOperationException(
                        String.Format(CultureInfo.CurrentCulture, "Action.{0} is invalid value.", this.Action));
            }
        }

        /// <summary>
        /// Gets the WorkItemCheckinInfo for current proxy object
        /// </summary>
        /// <returns></returns>
        internal WorkItemCheckinInfo GetWorkItemCheckinInfo()
        {
            WorkItemCheckinInfo result = new WorkItemCheckinInfo(
                this.WorkItem.TfsWorkItem,
                this.GetWorkItemCheckinAction());

            return result;
        }
    }

    /// <summary>
    /// The list of WorkItemCheckInInfoProxy
    /// </summary>
    [Serializable]
    public class WorkItemCheckInInfoProxyCollection : List<WorkItemCheckInInfoProxy>
    {
        internal WorkItemCheckinInfo[] GetWorkItemCheckinInfoArray()
        {
            WorkItemCheckinInfo[] workItemCheckinInfos = new WorkItemCheckinInfo[this.Count];

            for (int i = 0; i < this.Count; i++)
            {
                WorkItemCheckinInfo workItemCheckinInfo = this[i].GetWorkItemCheckinInfo();
                workItemCheckinInfos[i] = workItemCheckinInfo;
            }

            return workItemCheckinInfos;
        }
    }

    /// <summary>
    /// Check in action
    /// </summary>
    public enum TFSWorkItemCheckinAction
    {
        /// <summary></summary>
        None = 0,
        /// <summary></summary>
        Resolve = 1025,
        /// <summary></summary>
        Associate = 1026
    }
}


